import os
import cv2
from utils import ReadXml
from tqdm import tqdm

# ======================================================================函数功能
# 将标签图片裁剪出来

# ======================================================================参数设置begin
SrcDir = r'J:\ImgOutput\BackUp\Circle\RemoveLimit\Append\1103'  # 源文件路径
DstDir = r'J:\Temp\Temp1\1103'  # 输出文件路径
# ======================================================================参数设置end

names = []
Cnt = 0
a = os.scandir(SrcDir)


for files in os.scandir(SrcDir):
    if files.name[-1] == 'l':
        names.append(files.name)

for file_name in tqdm(names):
    img = cv2.imread(SrcDir + "\\" + file_name[:-4] + '.jpg')
    img_cp = img.copy()
    i_h, i_w, _ = img.shape
    locations = ReadXml(SrcDir + "\\" + file_name)
    for location in locations:
        LabelImg = img_cp[location[1]:location[3], location[0]:location[2]]
        LabelImg300300 = cv2.resize(LabelImg, (300, 300))
        Cnt += 1
        cv2.imwrite(DstDir + "\\%s_%d_%d_%d_%d_%s.jpg" % (
            file_name[:-4], location[0], location[1], location[2], location[3], location[-1]), LabelImg300300)
        # cv2.imshow('new', LabelImg)
        # cv2.waitKey(0)

print("-----------------------共%d张图片----------------------------" % Cnt)
print("------------------------------done-------------------------------")
